#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

from PySide import QtCore, QtGui
import os
from .pyuic.list_view_thumb_widget import Ui_list_view_thumb_widget
from .list_view_delegate import ListViewWidget, ListViewDelegate
from .pyuic import resources_rc


class ListViewThumbWidget(ListViewWidget):
    def __init__(self, parent):
        ListViewWidget.__init__(self, Ui_list_view_thumb_widget, parent)

    def set_text(self, header, body):
        msg = "<b style='color:#2C93E2'>%s</b><br>%s" % (header, body)
        self.ui.label.setText(msg)

    @staticmethod
    def calculate_size(scale_factor):
        # the thumbnail proportions are 512x400
        # add another 34px for the height so the text can be rendered.
        return QtCore.QSize(scale_factor, (scale_factor*0.78125)+34)
        

class ListViewThumbDelegate(ListViewDelegate):
    def _create_widget(self, parent):
        return ListViewThumbWidget(parent)

    def _format_folder(self, data, widget):
        header_text = data.name
        details_text = ''
        widget.set_text(header_text, details_text)

    def _format_publish(self, data, widget):
        header_text = data.name + '\t' + data.version
        details_text = data.asset_step.capitalize()
        widget.set_text(header_text, details_text)

    def sizeHint(self, style_options, model_index):
        """
        Specify the size of the item.
        :param style_options: QT style options
        :param model_index: Model item to operate on
        """
        # base the size of each element off the icon size property of the view
        scale_factor = self._view.iconSize().width()
        return ListViewThumbWidget.calculate_size(scale_factor)

    def _on_before_paint(self, widget, model_index, style_options):
        """
        :param widget: The widget to operate on (created via _create_widget)
        :param model_index: The model index to operate on
        :param style_options: QT style options
        """
        is_selected = self._view.selectionModel().isSelected(model_index)
        widget.set_selected(is_selected)

        # Only show actions if the selection only has a single item AND this item is the one selected.
        if is_selected and len(self._view.selectionModel().selectedIndexes()) == 1:
            widget.set_button_visible(True)
        else:
            widget.set_button_visible(False)
        # set thumbnail
        item = self._view.model().itemFromIndex(model_index)
        data = item.data(3)
        if data.item_type in ['library', 'type', 'asset']:
            icon = QtGui.QPixmap(":/icons/folder_512x400.png")
            self._format_folder(data, widget)
        else:
            icon = QtGui.QPixmap(os.path.splitext(data.path)[0] + '.png')
            self._format_publish(data, widget)
        width = self._view.iconSize().width()
        height = self._view.iconSize().height()
        thumb = icon.scaled(QtCore.QSize(width, height), QtCore.Qt.KeepAspectRatio,
                            QtCore.Qt.SmoothTransformation)
        widget.set_thumbnail(thumb)